<?php

class login_handler extends _site_handler {
	public $Username = '';
	public $Errors;

	public function __construct($m, $view) {
		$this->InitializeSite($m);
		$this->Wordlets->AddWordlets('login');

		if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
			$usr = new User();
			$usr->SetValuesByLogin($_POST['username']);
			if ( !$usr->Validate($_POST['password']) ) {
				$this->Errors['username'][] = $this->Wordlets->Get('error_username_password_invalid');
			} elseif ( @$_POST['test'] ) {
				$this->RenderViewContent('success');
				exit;
			} else {
				$_SESSION['user'] = $usr->Id();
				header("Location: " . (($_GET['back_url'])?$_GET['back_url']:'/'));
				exit;
			}
		}

		$this->RenderViewContent($view);
	}
}
